/*
 * Created on 2006-4-5
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sitech.portal.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;
import com.sitech.portal.system.domain.TblSYS_ROLE;
import com.sitech.portal.system.form.RoleForm;
import com.sitech.portal.system.service.ISysRoleModuleService;
import com.sitech.portal.system.service.ISysRoleService;

/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class RoleAction extends JGridBaseAction {
	
	private ISysRoleService sysRoleService;
	private ISysRoleModuleService sysRoleModuleService;
	private List rootList;
	private RoleForm[] dto ;
	private RoleForm roleForm;
	private String flag;
	
	public ISysRoleService getSysRoleService() {
		return sysRoleService;
	}

	public void setSysRoleService(ISysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}

	public ISysRoleModuleService getSysRoleModuleService() {
		return sysRoleModuleService;
	}

	public void setSysRoleModuleService(ISysRoleModuleService sysRoleModuleService) {
		this.sysRoleModuleService = sysRoleModuleService;
	}

	public List getRootList() {
		return rootList;
	}

	public void setRootList(List rootList) {
		this.rootList = rootList;
	}

	public RoleForm[] getDto() {
		return dto;
	}

	public void setDto(RoleForm[] dto) {
		this.dto = dto;
	}

	public RoleForm getRoleForm() {
		return roleForm;
	}

	public void setRoleForm(RoleForm roleForm) {
		this.roleForm = roleForm;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	/**
	 * 角色树列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String listRoleTree() {
		String nodeID = this.getRequest().getParameter("nodeID");
		String rootID = this.getRequest().getParameter("rootID");

		if ((rootID == null) || (nodeID == null)) {
			nodeID = "1";
			rootID = "0";
		}

		rootList = sysRoleService.findNoPageByParentRoleId(rootID);

		return SUCCESS;
	}

	public String loadRoleTree() {
		String nodeID = this.getRequest().getParameter("nodeID");// 其父节点ID
		String rootID = this.getRequest().getParameter("rootID");// 要传入的索引
		if ((rootID == null) || (nodeID == null)) {
			nodeID = "1";
			rootID = "0";
		}
		List list = new ArrayList();
		TblSYS_LOGINMSG user = this.getCurrentUser();
		String DeptID = ((String) user.getDEPT_ID()).substring(0, 3);
		// if("100".equals(DeptID)){
		// list = dao.findForListToAll();
		// }else{
		// list = dao.findForListByDeptID();
		// }
		// dao.initPagination(this.getPaginater()
		// .initPagination(request));
		list = sysRoleService.findNoPageByParentRoleId(rootID);
		if (list != null && list.size() > 0) {
			dto = new RoleForm[list.size()];
			for (int i = 0; i < list.size(); i++) {
				HashMap map = (HashMap) list.get(i);
				dto[i] = new RoleForm();
				dto[i].setROLE_ID(map.get("ROLE_ID").toString());
				dto[i].setROLE_NAME(map.get("ROLE_NAME").toString());
				dto[i].setROLE_DESC(map.get("ROLE_DESC").toString());
				dto[i].setCount(sysRoleService.findCountByParentRoleId(dto[i].getROLE_ID()));
			}
		}
		return SUCCESS;//
	}

	/**
	 * 角色编辑页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String editRole() throws Exception{
		
		String cmd = roleForm.getISMP_JSP_COMMAN();
		
		if(cmd.equals("modify")){
			String roleId = roleForm.getROLE_ID();
			if (roleId != null) {
				TblSYS_ROLE obj = sysRoleService.findByPK(roleId);
				try {
					BeanUtils.copyProperties(roleForm, obj);
				} catch (Exception be) {
					throw new Exception(be);
				}
				roleForm.setROLE_ID(roleId);
			}
		}

		roleForm.setISMP_JSP_COMMAN(cmd);
		return SUCCESS;
	}

	/**
	 * 角色保存页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String saveRole(){

		TblSYS_ROLE obj = new TblSYS_ROLE();

		flag = "true";
		try {
			BeanUtils.copyProperties(obj, roleForm);
		
			TblSYS_ROLE r = sysRoleService.findByPK(obj.getROLE_ID());
			if (r != null) {
				sysRoleService.update(obj);
			} else {
				int newRoleId = 0;
				List roles = sysRoleService.findNoPageByParentRoleId(obj.getPARENT_ID());
				if(roles == null || roles.size() == 0){
					obj.setROLE_ID(obj.getPARENT_ID() + "01");
				}else{
					newRoleId = Integer.parseInt(((HashMap)roles.get(roles.size()-1)).get("ROLE_ID").toString()) + 1;
					obj.setROLE_ID(String.valueOf(newRoleId));
				}
				sysRoleService.add(obj);
				
			}
		} catch (Exception be) {
			flag = be.getMessage();
		}
		return SUCCESS;
	}

	/**
	 * 删除角色
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String delRole() {

		flag = "true";
		
		String rolId = roleForm.getROLE_ID();

		try {
			sysRoleService.deleteRole(rolId);
			sysRoleModuleService.deleteByRoleId(rolId);
			
		} catch (Exception e) {
			flag = e.getMessage();
		}

		return SUCCESS;
	}

	/**
	 * 显示角色列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String loadListRole() throws Exception{
//		try {
//			roleForm.setPARENT_NODE_NAME(URLDecoder.decode(URLDecoder.decode(roleForm.getPARENT_NODE_NAME(), "UTF-8"), "UTF-8"));
//		} catch (UnsupportedEncodingException e) {
//			throw new AppException("解码异常",e);
//		}
		return SUCCESS;
	}
	public String loadListRoleQueryJson() {

		IPage lst = null;

		if (roleForm.getPARENT_ID() != null && !"".equals(roleForm.getPARENT_ID())) {
			lst = sysRoleService.findByParentRoleId(roleForm.getPARENT_ID(), this.getPageForm());
		} else {
			lst = sysRoleService.findForList(this.getPageForm());
		}

		this.setResult(lst);

		return SUCCESS;
	}

	// public ActionForward loadRoleManage(ActionMapping mapping, ActionForm
	// form,
	// HttpServletRequest request, HttpServletResponse response)
	// throws BaseException {
	//    	
	// RoleForm theForm = (RoleForm)form;
	// String nodeID = request.getParameter("nodeID");
	// String rootID = request.getParameter("rootID");
	//
	// if ((rootID == null) || (nodeID == null)) {
	// nodeID = "1";
	// rootID = "";
	// }
	//    	
	// SYS_ROLEDao dao = new SYS_ROLEDao();
	// List rootList = dao.findFirstLevelForList();
	//    	
	// request.setAttribute("rootList", rootList);
	// request.setAttribute("ROLE_ID", rootID);
	//        	
	// return mapping.findForward("default");
	// }
	
	public String loadRoleManage(){
		return SUCCESS;
	}
	public String loadRoleManageIndex(){
		return SUCCESS;
	}

	/**
	 * 显示角色列表,角色流程树
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String loadRoleTreeForProcess() {
		String nodeID = this.getRequest().getParameter("nodeID");
		String rootID = this.getRequest().getParameter("rootID");
		if ((rootID == null) || (nodeID == null)) {
			nodeID = "1";
			rootID = "0";
		}
		rootList = sysRoleService.findNoPageByParentRoleId(rootID);
		return SUCCESS;
	}

	public String loadSubRoleTree() {
		String nodeID = this.getRequest().getParameter("nodeID");// 其父节点ID
		String rootID = this.getRequest().getParameter("rootID");// 要传入的索引
		if ((rootID == null) || (nodeID == null)) {
			nodeID = "1";
			rootID = "0";
		}
		List list = new ArrayList();
		TblSYS_LOGINMSG user = this.getCurrentUser();
		String DeptID = ((String) user.getDEPT_ID()).substring(0, 3);
		list = sysRoleService.findNoPageByParentRoleId(rootID);
		if (list != null && list.size() > 0) {
			dto = new RoleForm[list.size()];
			for (int i = 0; i < list.size(); i++) {
				HashMap map = (HashMap) list.get(i);
				dto[i] = new RoleForm();
				dto[i].setROLE_ID(map.get("ROLE_ID").toString());
				dto[i].setROLE_NAME(map.get("ROLE_NAME").toString());
				dto[i].setROLE_DESC(map.get("ROLE_DESC").toString());
				dto[i].setCount(sysRoleService.findCountByParentRoleId(dto[i].getROLE_ID()));
			}
		}
		return SUCCESS;//
	}
	
	public String findModuleIdByRoleId(){
		List result = sysRoleModuleService.findModuleIdByRoleId(roleForm.getROLE_ID());
		flag = JSONUtil.toJSON(result);
		return SUCCESS;
	}

}
